package com.worklikedance.priceonline;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.worklikedance.util.PopoverView;
import com.worklikedance.util.Utilities;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class RelativeLayoutEx extends RelativeLayout {
	protected Context mContext = null;
	
	protected boolean mManualFetchingData = false;
	protected boolean mFetchingData = false;
	
	protected PullToRefreshListView mPullToRefreshListView;
	protected ListView mListView;
	protected TextView mUpdatedTimeTextView;
	protected PopoverView mPopoverView = null;
	
	protected String mLatestUpadateDateTime = "";
	
	protected ProgressDialog mProgressDialog;
	
	protected static String NUMBER_FORMAT = "%.0f";
	
	public RelativeLayoutEx(Context context) {
		this(context, null);
	}

	public RelativeLayoutEx(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public RelativeLayoutEx(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		mContext = context;
	}
	
	public void dismissPopup() {
		if (mPopoverView == null) {
			return;
		}
		
		mPopoverView.dissmissPopover(true);			
	}
	
	protected void setValueTextViews(double curPrice, double prePrice, String formatFloat, TextView curTextView, TextView diffTextView, TextView perTextView, View gachNgang) {
		setValueTextViews(curPrice, prePrice, formatFloat, curTextView, null, diffTextView, perTextView, gachNgang);		
	}
	
	protected void setValueTextViews(double curPrice, double prePrice, String formatFloat, TextView curTextView, TextView preTextView, TextView diffTextView, TextView perTextView, View gachNgang) {		
		double diff = (curPrice - prePrice);
		
		double per = .0f;
		if (prePrice != .0f) {
			per = Math.abs(diff/prePrice);
		}

		int textColor = mContext.getResources().getColor(R.color.RED_TEXT_COLOR);
		
				
        if (curPrice > 0) {
        	String curString = String.format(formatFloat, curPrice);
        	curTextView.setText(Utilities.formatNumber(curString));
		}
        else {
			curTextView.setText("");
		}
        
        if (preTextView != null) {        	
            if (prePrice > 0) {
            	String preString = String.format(formatFloat, prePrice);
            	preTextView.setText(Utilities.formatNumber(preString));
    		}
            else {
    			preTextView.setText("");
    		}
		}        
		
		//text color
		if (curPrice >= prePrice) {
			textColor = mContext.getResources().getColor(R.color.GREEN_COLOR);
		}
		
		diffTextView.setTextColor(textColor);
		perTextView.setTextColor(textColor);
		
		// Hidden if ko co thay doi
		if (diff != 0) {			
			diffTextView.setVisibility(View.VISIBLE);
			perTextView.setVisibility(View.VISIBLE);
			
			if (gachNgang != null) {
				gachNgang.setVisibility(View.INVISIBLE);
			}				
		}				
		else {			
			//for cell
			if (gachNgang != null) {
				gachNgang.setVisibility(View.VISIBLE);
				
				diffTextView.setVisibility(View.INVISIBLE);
				perTextView.setVisibility(View.INVISIBLE);
			}
			else {
				//for callout
				diffTextView.setVisibility(View.VISIBLE);
				perTextView.setVisibility(View.VISIBLE);
			}
		}
		
		//preData = 0
		if (prePrice == 0) {
			if (gachNgang != null) {
				gachNgang.setVisibility(View.INVISIBLE);
			}
			
			diffTextView.setVisibility(View.INVISIBLE);
			perTextView.setVisibility(View.INVISIBLE);
		}
		
		//set value
		diffTextView.setText((diff >= 0? "+":"") + Utilities.formatNumber(String.format(formatFloat,diff)));
		perTextView.setText(" (" + String.format("%.1f", per*100) + "%)");			
	}

	protected void updateChangedTextView(double preValue, 
			double curValue, 
			TextView valueChangeTextView, 
			TextView percentChangeTextView,
			TextView changeTextView) {
		int textColor = mContext.getResources().getColor(R.color.black_pure);
		
		if (preValue < 1 || curValue < 1) {
			valueChangeTextView.setText("");
			percentChangeTextView.setText("");
			changeTextView.setTextColor( mContext.getResources().getColor(R.color.black_pure) );
			
			return;
		}

		if (curValue < preValue) {
			textColor = mContext.getResources().getColor(R.color.red);
		}
		else {
			textColor = mContext.getResources().getColor(R.color.green_changed_value);
		}
		
		double diff = (curValue - preValue);

		valueChangeTextView.setTextColor(textColor);
		valueChangeTextView.setText( (diff >= 0? "+":"") + String.format("%.1f", diff) );

		percentChangeTextView.setTextColor(textColor);
		percentChangeTextView.setText( String.format("(%.1f", Math.abs(diff*100/preValue)) + "%)");
		
		changeTextView.setTextColor( mContext.getResources().getColor(R.color.blue) );
	}
	
	public boolean isOnline() {
	    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
	    
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    
	    return false;
	}
	
	public void fetchData(boolean allowLoadingOfflineData) {
		//
	}
	
	protected void alert(String message) {
		Toast toast = Toast.makeText(mContext, message, Toast.LENGTH_SHORT);
		
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
	
	protected void showWaitingProgress(String message) {
		mProgressDialog = ProgressDialog.show(mContext, "", message);
	}

	protected void hideWaitingProgress() {
		try {
			if (mProgressDialog != null && mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();	
			}
		} 
		catch (Exception e) {
		}
		
		mProgressDialog = null;
	}
	
	protected void updateLastUpdatedTime(String time) {
		if (mUpdatedTimeTextView != null) {
			mUpdatedTimeTextView.setText(time);	
		}
	}
}
